<?php
require_once("dao/DAOFactory.php");
require_once("bizlayer/IBiz.php");


class DataMigrationMgmt implements IBiz{
	static $dao;
	const lineseparator = "\n";
	const fieldseparator = ",";
	
	public static function initializeDao(){
		self::$dao = DAOFactory::getDAO("datamigration");
	}
	
	public static function importResult(){
		self::initializeDao();
		self::$dao->importResult();
	}
	public static function buildSnapshot($eventid){
		self::initializeDao();
		self::$dao->buildSnapshot();
	}
	public static function displayContents($importStatusId){
		self::initializeDao();
		return self::$dao->displayContents($importStatusId);
	}
	public static function listNewPlayers($importStatusId){
		self::initializeDao();
		return self::$dao->listNewPlayers($importStatusId);
	}
	public static function updatePlayer($orig,$new){
		self::initializeDao();
		return self::$dao->updatePlayer($orig,$new);
	}
	public static function fillInIds(){
		self::initializeDao();
		self::$dao->fillInIds();
	}
	
	public static function insertNewPlayers(){
		self::initializeDao();
		self::$dao->insertNewPlayers();
	}
	
	public static function listNewCourses($importStatusId){
		self::initializeDao();
		return self::$dao->listNewCourses($importStatusId);
	}
	
	public static function importResults($importStatusId){
		self::initializeDao();
		self::$dao->importResults($importStatusId);
	}
	
	public static function getSnapshot($eventid){
		self::initializeDao();
		return self::$dao->getSnapshot($eventid);
	}
	
	public static function bulkInsert($filePath,$eventYear,$importStatusID){
		$qry = self::parseBulkInsertQuery($filePath,$eventYear,$importStatusID);
		if($qry != ""){
			self::initializeDao();
			self::$dao->bulkInsert($qry);
			return true;	
		}
		else{
			return false;
		}
		
	}
	
	private static function parseBulkInsertQuery($filePath,$eventYear,$importStatusID){
		$file = fopen($filePath,"r");
		$size = filesize($filePath);
		$csvcontent = fread($file,$size);
		fclose($file);
		
		$i = 0;
		$allrows = explode(self::lineseparator,trim($csvcontent)); 
		$aQry = array();
		foreach($allrows as $line){
			$i++;
			if($i > 1){
				$line = trim($line," \t");
				$line = str_replace("\r","",$line);
				$line = str_replace("'","\'",$line);
				
				$linearray = explode(self::fieldseparator,$line);
				$linemysql = array();
				foreach($linearray as $cell){
					$val = "";
					
					if(trim($cell) == ""){
						$val = "null";
					}
					elseif(is_numeric($cell)){
						$val = sprintf("%s",$cell);
					}
					else{
						$val = sprintf("'%s'",$cell);
					}
					array_push($linemysql,$val);
				}
				array_push($linemysql,$eventYear);
				array_push($linemysql,$importStatusID);
				
				if(count($linemysql) == 24){
					array_push($aQry,sprintf("SELECT %s\n",implode(",",$linemysql)));
					if($i < count($allrows))
						array_push($aQry,"UNION\n");
				}
				else{
					$success = false;
					$msg = "Invalid data format detected. Extra field(s) found. Please review it.";
					header(sprintf("Location:../admin/imptwiz2_upload.php?uploaded=%s&msg=%s",$success,$msg));	
				}
			}
		}
		$output = "";
		if(count($aQry) > 0){
			$output = "INSERT INTO denormalized_results(firstname,lastname,email_address,Course,`1`,`2`,`3`,`4`,`5`,`6`,`7`,`8`,`9`,`10`,`11`,`12`,`13`,`14`,`15`,`16`,`17`,`18`,`year`,import_status_id)\n";
			$output .= implode("",$aQry);
		}
		return $output;	
	}
}
?>
